package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private SetmealService setmealService;

    @Reference
    private ReportService reportService;

    /**
     * 会员数量统计
     * @return
     */
    @RequestMapping("/getMemberReport")
     public Result getMemberReport(){
        Calendar calendar = Calendar.getInstance();//获取到一个calendar对象,getTime为获取当前时间
        calendar.add(Calendar.MONTH,-12);//获取当前日期之前12个月的日期

        List<String> list= new ArrayList<>();
        for (int i = 0; i <12; i++) {
            calendar.add(Calendar.MONTH,1);//每次循环向后前加一个月
            //将当月的时间转换为yyyy.MM格式
            list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        Map<Object, Object> map = new HashMap<>();
        map.put("month",list);
        //调用memberService查询每一个月会员数量
     List<Integer> memberCount =  memberService.findMemberCountByMonth(list);
         map.put("memberCount",memberCount);
         return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
       List<Map<String,Object>> list = setmealService.findSetmealCount();

       Map<String,Object> map=new HashMap<>();
       map.put("setmealCount",list);

      List<String> setmealNames= new ArrayList<>();
        for (Map<String, Object> m : list) {
            String name = (String) m.get("name");
            setmealNames.add(name);
        }
        map.put("setmealNames",setmealNames);
        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }

    /**
     * 获取运营统计数据
     *
     */
        @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() throws Exception {
           Map<String,Object> result= reportService.getBusinessReport();
            try {
                return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,result);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
            }
        }


}

